package com.huike.game.listview;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.huike.game.R;

public class PullRefresh extends ListView {

    private View inflate;
    private int height;

    private static final int jPULL = 0;
    private static final int jSTATUS = 1;
    private static final int jEND = 2;

    private int currentStatus = jPULL;
    private RotateAnimation animUp;
    private RotateAnimation animDow;
    private RotateAnimation animation;
    private ImageView iv_pull_game59;
    private TextView tv_name_game59;

    public PullRefresh(Context context) {
        this(context, null);
    }

    public PullRefresh(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public PullRefresh(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initHeard(context);

    }

    private void initHeard(Context context) {

        inflate = View.inflate(context, R.layout.refresh_header, null);
        iv_pull_game59 = inflate.findViewById(R.id.iv_pull_game59);
        tv_name_game59 = inflate.findViewById(R.id.tv_name_game59);

        inflate.measure(0, 0);
        height = inflate.getMeasuredHeight();
        inflate.setPadding(0, -height, 0, 0);
        addHeaderView(inflate);

        initAnimation();

    }

    private int start = -1;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                start = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int end = (int) ev.getY();
                int dy = end - start;

                int firstVisiblePosition = this.getFirstVisiblePosition();
                if (dy > 0 && firstVisiblePosition == 0) {

                    int padidng = -height + dy;


                    if (padidng >= 0 && currentStatus != jSTATUS) {
                        currentStatus = jSTATUS;
                        refreshState();
                    } else if (padidng <= 0 && currentStatus != jPULL) {
                        currentStatus = jPULL;
                        refreshState();

                    }
                    inflate.setPadding(0,padidng,0,0);

                    return true;
                }


                break;
            case MotionEvent.ACTION_UP:

                if (currentStatus == jSTATUS) {
                    currentStatus = jEND;
                    inflate.setPadding(0, 0, 0, 0);
                    refreshState();
                } else if (currentStatus == jPULL) {
                    currentStatus = jPULL;
                    inflate.setPadding(0, -height, 0, 0);
                    refreshState();

                }

                break;
        }

        return super.onTouchEvent(ev);
    }


    private void initAnimation() {
        animUp = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animDow = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animDow.setFillAfter(true);
        animDow.setDuration(300);

        animUp.setFillAfter(true);
        animUp.setDuration(300);

        animation.setDuration(300);
        animation.setRepeatCount(-1);
        animation.setInterpolator(new LinearInterpolator());
    }

    private void refreshState() {

        if (currentStatus == jPULL) {
            tv_name_game59.setText("下拉刷新");
            iv_pull_game59.startAnimation(animDow);

        } else if (currentStatus == jSTATUS) {
            tv_name_game59.setText("即将刷新");
            iv_pull_game59.startAnimation(animUp);

        } else if (currentStatus == jEND) {
            tv_name_game59.setText("正在刷新");
            iv_pull_game59.setImageResource(R.drawable.refresh);
            iv_pull_game59.startAnimation(animation);
                if(lisener != null){
                    lisener.onRefresh();
                }
        }

    }

    public void setComplenerAnimation(){
        tv_name_game59.setText("刷新完成");
        iv_pull_game59.clearAnimation();
        iv_pull_game59.setImageResource(R.drawable.obu);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                tv_name_game59.setText("下拉刷新");
                iv_pull_game59.clearAnimation();
                iv_pull_game59.setImageResource(R.drawable.rjd_down);
                currentStatus = jPULL;

                inflate.setPadding(0,-height,0,0);
            }
        },2000);

    }

    private onRefreshLisener lisener;
    public void setOnRefreshLisener(onRefreshLisener lisener){
        this.lisener = lisener;
    }
    public interface onRefreshLisener{
        void onRefresh();
    }


}
